package com.example.taobaounion3.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.taobaounion3.R;
import com.example.taobaounion3.utils.LogUtils;

import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public abstract class baseFragment extends Fragment {

    private State currentState=State.NONE;
    private View mSuccessView;
    private View mLoadingView;
    private View mErrorView;
    private View mEmptyView;

    public enum State{
        NONE,LOADING,SUCCESS,ERROR,EMPTY
    }
    private Unbinder mBind;
    private FrameLayout mBaseContainer;

    @OnClick(R.id.network_error_tips)
    public void retry(){
        //点击了重新加载内容...（这里可能有问题）
        LogUtils.d(this,"on retry...");
        onRetryClick();
    }
    //子类可以重写实现点击效果
    protected void onRetryClick() {

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View rootView = loadRootView(inflater,container);
        mBaseContainer=rootView.findViewById(R.id.base_container);
        loadStatesView(inflater,container);
        //View successView=loadRootView(inflater,container,savedInstanceState);

        mBind=ButterKnife.bind(this,rootView);
        initView(rootView);
        initListener();
        initpresenter();
        loadDate();

        return rootView;
    }

    //如果子类需要去设置相关的事件，覆盖此方法即可
    protected void initListener() {

    }

    protected View loadRootView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.base_fragment_layout, container, false);
    }


    //加载各种状态的View
    private void loadStatesView(LayoutInflater inflater, ViewGroup container) {
        //成功的View
        mSuccessView=loadSuccessView(inflater,container);
        mBaseContainer.addView(mSuccessView);

        //Loading的View
        mLoadingView=loadLoadingView(inflater,container);
        mBaseContainer.addView(mLoadingView);

        //错误页面
        mErrorView=loadErrorView(inflater,container);
        mBaseContainer.addView(mErrorView);

        //空页面
        mEmptyView=loadEmptyView(inflater,container);
        mBaseContainer.addView(mEmptyView);

        setUpState(State.NONE);

    }

    protected View loadErrorView(LayoutInflater inflater, ViewGroup container) {
       return inflater.inflate(R.layout.fragment_error,container,false);
    }

    protected View loadEmptyView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_empty,container,false);
    }

    protected View loadLoadingView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_loading,container,false);
    }

    //子类通过这个方法切换状态
    public void setUpState(State state){
        this.currentState=state;
        if (currentState==State.SUCCESS){
            mSuccessView.setVisibility(View.VISIBLE);
        }else {
            mSuccessView.setVisibility((View.GONE));
        }
        if (currentState==State.LOADING){
            mLoadingView.setVisibility(View.VISIBLE);
        }else {
            mLoadingView.setVisibility((View.GONE));
        }
        mErrorView.setVisibility(currentState==State.ERROR?View.VISIBLE:View.GONE);
        mEmptyView.setVisibility(currentState==State.EMPTY?View.VISIBLE:View.GONE);

    }

    protected void initView(View rootView) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mBind!=null){
            mBind.unbind();
        }
        release();

    }

    protected void release() {
        //释放资源
    }

    protected void initpresenter() {
        //创建presenter

    }

    protected void loadDate() {
        //加载数据

    }


    private View loadSuccessView(LayoutInflater inflater,ViewGroup container) {
        //加载View
        int resId=getRootViewResId();
        return  inflater.inflate(resId,container,false);
    }

    protected abstract int getRootViewResId();


}
